package service;

import JDBC.DBUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import method.AccountInformation;
import method.Encryption;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
/**
 * @author Gao Shiwei
 * user can login in from here
 */
@WebServlet(name = "Login")
public class LoginIn extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", req.getHeader("Origin"));
        resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, If-Modified-Since");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
        resp.setHeader("Access-Control-Max-Age", "3600");
        resp.addHeader("Access-Control-Allow-Credentials", "true");
        String PhoneNumber=req.getParameter("PhoneNumber");
        String passwordStr=req.getParameter("Password");
        String password = "";
        try {
            password = Encryption.aesDecodeStr(passwordStr,"wwwwwwwwwwwwwww1wwwwwwwwwwwwwww1");
        } catch (Exception e) {
            e.printStackTrace();
        }

        //System.out.println(PhoneNumber+"zzzz");
        String answer=DBUtils.check(PhoneNumber,password);
        String name=null;
        if(answer.equals("true")){
            name=DBUtils.FindName(PhoneNumber);
        }
        PrintWriter out;
        boolean state=answer.equals("true");
        //System.out.println(state);
        //System.out.println(str+"666");
        HttpSession session = req.getSession();
        //System.out.println(PhoneNumber);
        session.setAttribute("PhoneNumber",PhoneNumber);
        session.setAttribute("Password",password);

        // 去掉cookie的httpOnly 让js可以获取cookie
        String header = resp.getHeader("Set-Cookie");
        int index;
        if(header !=null && (index = header.indexOf("HttpOnly"))>0) {
            header = header.substring(0,index - 1);
            resp.setHeader("Set-Cookie",header);
        }
        //System.out.println(session.getAttribute("PhoneNumber")+"zzzz");
        if(answer.equals("true")) {
            //System.out.println("wdasdw");
            AccountInformation detail = DBUtils.QueryAccountInformation(PhoneNumber);
            JsonObject Container=new JsonObject();

            Container.addProperty("answer",answer);

            JsonArray array=new JsonArray();

            JsonObject oneItem=new JsonObject();

                oneItem.addProperty("PhoneNumber",detail.getPhoneNumber());
                oneItem.addProperty("Deposit",detail.getDeposit());
                oneItem.addProperty("AccountName",detail.getAccountName());
                oneItem.addProperty("RealName",detail.getRealName());
                oneItem.addProperty("CreateTime", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(detail.getCreateTime()));
                array.add(oneItem);
                System.out.println(array);
            Container.add("AccountInformation",array);
            out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
            }
        else{
            //System.out.println("wwwww");
            JsonObject Container=new JsonObject();

            Container.addProperty("answer",answer);
            out = resp.getWriter();
            out.println(Container);
            out.flush();
            out.close();
        }
        }
            //session.setMaxInactiveInterval(60);
    }
